﻿using CommandProcessor.Command;
using Data.Infrastructure;
using Data.Models;
using Data.Repositories;
using Domain.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain.Handlers
{
    public class CreateOrUpdateMaterialHandler : ICommandHandler<CreateOrUpdateMaterialCommand>
    {
        private readonly IMaterialRepository materialRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateMaterialHandler(IMaterialRepository materialRepository, IUnitOfWork unitOfWork)
        {
            this.materialRepository = materialRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateMaterialCommand command)
        {
            var material = new Material
            {
                ID = command.ID,
                Description = command.Description,
                Name = command.Name,
                Code=command.Code,
                MaterialTypeID=command.MaterialTypeID
            };
            if (material.ID == 0)
                materialRepository.Add(material);
            else
                materialRepository.Update(material);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateMaterialCommand> commands)
        {
            throw new NotImplementedException();
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateMaterialCommand> commands)
        {
            throw new NotImplementedException();
        }
    }
}
